package com.newjiabo.framework.security.filter;

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.newjiabo.common.core.enums.UserEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import com.newjiabo.common.core.domain.model.LoginUser;
import com.newjiabo.common.utils.SecurityUtils;
import com.newjiabo.common.utils.StringUtils;
import com.newjiabo.framework.web.service.TokenService;

/**
 * token过滤器 验证token有效性
 *
 * @author CaoMengHui
 */
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    @Resource
    private TokenService tokenService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException {
        // TODO <CaoMengHui>: 改造security的拦截逻辑
        String token = tokenService.getToken(request);
        if(StringUtils.isEmpty(token)){
            chain.doFilter(request, response);
            return;
        }
        String requestURI = request.getRequestURI();
        String projectUrlHead = requestURI.substring(requestURI.indexOf("/") + 1, StringUtils.ordinalIndexOf(requestURI, "/", 2));
        if (StringUtils.isNotEmpty(token) && StringUtils.isNull(SecurityUtils.getAuthentication())) {
            UsernamePasswordAuthenticationToken authenticationToken = null;
            switch (projectUrlHead){
                case "system":
                    LoginUser loginUser = tokenService.getLoginUser(request, UserEnums.SYSTEM);
                    tokenService.verifyToken(loginUser);
                    authenticationToken = this.getAuthenticationSystem(loginUser,request);
                    break;
                case "applet":
                    loginUser = tokenService.getLoginUser(request, UserEnums.MEMBER);
                    tokenService.verifyToken(loginUser);
                    authenticationToken = this.getAuthenticationSystem(loginUser,request);
                    break;
                case "tool":
                    loginUser = tokenService.getLoginUser(request, UserEnums.SYSTEM);
                    tokenService.verifyToken(loginUser);
                    authenticationToken = this.getAuthenticationSystem(loginUser,request);
                    break;
            }
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        }
        chain.doFilter(request, response);
    }

    public UsernamePasswordAuthenticationToken getAuthenticationSystem(LoginUser loginUser,HttpServletRequest request){
        UsernamePasswordAuthenticationToken  authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        return authenticationToken;
    }
}
